////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.index;

import java.util.List;

import android.os.Bundle;
import android.text.Html;
import android.text.util.Linkify;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.IndexGroup;

/**
 * @author haibo for list item -- 0, 1, 3
 */
public class IndexOptionContentActivity extends HttpSuperClassStockBaseActivity
{
	protected static final String	TAG	= IndexOptionContentActivity.class.getSimpleName();
	public static String			_title;

	public String					_content;

	private TextView				_txt;
	private TextView				_titleTextView;
	private Button					_toolBtnReturn;

	// public static final String KEY_CONTENT = "key.content";
	// public static final String KEY_TITLE = "key.content";
	// public static final String KEY_CLASS = "key.content";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index_option_content);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");

				((ITDLApplication) getApplicationContext()).putString(CommonDefn.ACTIVITY_CAME_FROM_KEY, TAG);

				// Intent intent = new Intent(getApplicationContext(), _cls);
				// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
				// Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				// SwitchForwardActivity(_cls, intent);

				SwitchBackActivity();
			}
		});

		_txt = (TextView) findViewById(R.id.list_item_content);
		// _txt.setAutoLinkMask(Linkify.ALL);
		_txt.setAutoLinkMask(Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES);
		_titleTextView = (TextView) findViewById(R.id.more_menu_item_title);
	}

	@Override
	protected void onResume()
	{
		super.onResume();

		_titleTextView.setText(_title);
	}

	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return IndexGroup.GetActivityGroupHostControl();
	}

	static public int	iTypeId	= 0;

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		_txt.setText("");
		return HttpAppInfo.SendIndexOptionContentRequest(actContext, iTypeId, this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(H_UserClient_HttpRequest_Result iResult)
	{
		if (!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
			return;
		}

		updateData(iResult.contentStores);
	}

	private void updateData(List<String[]> _list)
	{
		if (_list.size() <= 0)
		{
			_txt.setText(R.string.provide_no_information);
			return;
		}

		if (iTypeId == 0)
		{
			_content = _list.get(0)[0];
		}
		else if (iTypeId == 2)
		{
			_content = _list.get(0)[0].replaceAll(" ", "<br>");
		}
		else if (iTypeId == 3)
		{
			StringBuffer buffer = new StringBuffer();
			buffer.append("歐洲夏令時間")
					.append("<br>")
					.append("開始日期:")
					.append(_list.get(0)[1].substring(5, 7))
					.append("月")
					.append(_list.get(0)[1].substring(8, 10))
					.append("<br>")
					.append("結束時間:")
					.append(_list.get(0)[2].substring(5, 7))
					.append("月")
					.append(_list.get(0)[2].substring(8, 10))
					.append("<br>")
					.append("開市時間由香港時間")
					.append("<br>")
					.append(_list.get(0)[3].substring(0, 5))
					.append("改為")
					.append(_list.get(0)[4].substring(0, 5))
					.append("<br>")
					.append("<br>")
					.append("<br>")

					.append("美盤夏令時間")
					.append("<br>")
					.append("開始日期:")
					.append(_list.get(1)[1].substring(5, 7))
					.append("月")
					.append(_list.get(1)[1].substring(8, 10))
					.append("<br>")
					.append("結束時間:")
					.append(_list.get(1)[2].substring(5, 7))
					.append("月")
					.append(_list.get(1)[2].substring(8, 10))
					.append("<br>")
					.append("開市時間由香港時間")
					.append("<br>")
					.append(_list.get(1)[3].substring(0, 5))
					.append("改為")
					.append(_list.get(1)[4].substring(0, 5))
					.append("<br>")
					.append("<br>");
			_content = buffer.toString();
		}

		_txt.setText(Html.fromHtml(_content.replaceAll("[\n\r]", "<br>").replaceAll("&lt;", "<").replaceAll("&gt;", ">")));
	}

}
